package com.tangzx.builder;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import com.tangzx.builder.context.IAbcContext;
import com.tangzx.builder.decoder.MovieABCDecoder;
import com.tangzx.builder.encoder.MovieABCEncoder;
import flash.swf.TagDecoder;
import flash.swf.TagEncoder;
import flash.swf.tags.ShowFrame;

public class MovieMixer {
	private IAbcContext abcContext;
	private FileInputStream file;
	private SwfMovie swf;
	private TagEncoder tagEncoder;
	private MovieABCEncoder abcEncoder;
	
	public MovieMixer(IAbcContext abcContext, FileInputStream file) {
		this.abcContext = abcContext;
		this.file = file;
	}
	
	public void parse() {
		try {
			swf = new SwfMovie();
			TagDecoder tagDecoder = new TagDecoder(file);
			MovieABCDecoder abcDecoder = new MovieABCDecoder(swf, abcContext);
			tagDecoder.parse(abcDecoder);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void mix() {
		tagEncoder = new TagEncoder();
		abcEncoder = new MovieABCEncoder(tagEncoder, abcContext);
		abcEncoder.mix(swf);
	}

	public void writeTo(FileOutputStream out) {
		try {
			abcEncoder.export(swf);
			tagEncoder.writeTo(out);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}